home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / sprite / foo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  27.4 KB  |  1,344 lines

  1. /* sparc.c -- Assemble for the SPARC
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #include "sparc-opcode.h"
  25. #include "as.h"
  26. #include "frags.h"
  27. #include "struc-symbol.h"
  28. #include "flonum.h"
  29. #include "expr.h"
  30. #include "hash.h"
  31. #include "md.h"
  32. #include "sparc.h"
  33. #include "write.h"
  34. #include "read.h"
  35. #include "symbols.h"
  36.  
  37. void md_begin();
  38. void md_end();
  39. void md_number_to_chars();
  40. void md_assemble();
  41. char *md_atof();
  42. void md_convert_frag();
  43. void md_create_short_jump();
  44. void md_create_long_jump();
  45. int  md_estimate_size_before_relax();
  46. void md_number_to_imm();
  47. void md_number_to_disp();
  48. void md_number_to_field();
  49. void md_ri_to_chars();
  50. void emit_relocations();
  51. static void sparc_ip();
  52.  
  53. relax_typeS md_relax_table[] = { 0 };
  54.  
  55. /* handle of the OPCODE hash table */
  56. static struct hash_control *op_hash = NULL;
  57.  
  58. static void s_seg(), s_proc(), s_data1(), s_reserve(), s_common();
  59. extern void s_globl(), s_long(), s_short(), s_space(), cons();
  60.  
  61. pseudo_typeS
  62. md_pseudo_table[] = {
  63.     { "common",     s_common,   0 },
  64.     { "global",     s_globl,    0 },
  65.     { "half",       cons,       2 },
  66.     { "proc",       s_proc,     0 },
  67.     { "reserve",    s_reserve,  0 },
  68.     { "seg",        s_seg,      0 },
  69.     { "skip",       s_space,    0 },
  70.     { "word",       cons,       4 },
  71.     { NULL,         0,          0 },
  72. };
  73.  
  74. int md_short_jump_size = 4;
  75. int md_long_jump_size = 4;
  76. int omagic  =  (0x103 << 16) | OMAGIC;  /* Magic number for header */
  77.  
  78. /* This array holds the chars that always start a comment.  If the
  79.     pre-processor is disabled, these aren't very useful */
  80. char comment_chars[] = "!";    /* JF removed '|' from comment_chars */
  81.  
  82. /* This array holds the chars that only start a comment at the beginning of
  83.    a line.  If the line seems to have the form '# 123 filename'
  84.    .line and .file directives will appear in the pre-processed output */
  85. /* Note that input_file.c hand checks for '#' at the beginning of the
  86.    first line of the input file.  This is because the compiler outputs
  87.    #NO_APP at the beginning of its output. */
  88. /* Also note that '/*' will always start a comment */
  89. char line_comment_chars[] = "#";
  90.  
  91. /* Chars that can be used to separate mant from exp in floating point nums */
  92. char EXP_CHARS[] = "eE";
  93.  
  94. /* Chars that mean this number is a floating point constant */
  95. /* As in 0f12.456 */
  96. /* or    0d1.2345e12 */
  97. char FLT_CHARS[] = "rRsSfFdDxXpP";
  98.  
  99. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  100.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  101.    but nothing is ideal around here.
  102.  */
  103. int size_reloc_info = sizeof(struct reloc_info_sparc);
  104.  
  105. static unsigned char octal[256];
  106. #define isoctal(c)  octal[c]
  107. static unsigned char toHex[256];
  108.  
  109. /*
  110.  *  anull bit - causes the branch delay slot instructions to not be executed 
  111.  */
  112. #define ANNUL       (1 << 29)
  113.  
  114. struct sparc_it {
  115.     char    *error;
  116.     unsigned long opcode;
  117.     struct nlist *nlistp;
  118.     expressionS exp;
  119.     int pcrel;
  120.     enum reloc_type reloc;
  121. } the_insn, set_insn;
  122.  
  123. #ifdef __STDC__
  124. static void print_insn(struct sparc_it *insn);
  125. static int getExpression(char *str);
  126. #else
  127. static void print_insn();
  128. static int getExpression();
  129. #endif
  130. static char *expr_end;
  131. static int special_case;
  132.  
  133. #define SPECIAL_CASE_SET    1
  134.  
  135. /*
  136.  * sort of like s_lcomm
  137.  *
  138.  */
  139. static void
  140. s_reserve()
  141. {
  142.     char *name;
  143.     char c;
  144.     char *p;
  145.     int temp;
  146.     symbolS *symbolP;
  147.  
  148.     name = input_line_pointer;
  149.     c = get_symbol_end();
  150.     p = input_line_pointer;
  151.     *p = c;
  152.     SKIP_WHITESPACE();
  153.     if ( * input_line_pointer != ',' ) {
  154.     as_warn("Expected comma after name");
  155.     ignore_rest_of_line();
  156.     return;
  157.     }
  158.     input_line_pointer ++;
  159.     if ((temp = get_absolute_expression()) < 0) {
  160.     as_warn("BSS length (%d.) <0! Ignored.", temp);
  161.     ignore_rest_of_line();
  162.     return;
  163.     }
  164.     *p = 0;
  165.     symbolP = symbol_find_or_make(name);
  166.     *p = c;
  167.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  168.     as_warn("bad .reserve segment: `%s'", input_line_pointer);
  169.     return;
  170.     }
  171.     input_line_pointer += 6;
  172.     if (symbolP->sy_other == 0 
  173.         && symbolP->sy_desc  == 0
  174.     && ((symbolP->sy_type  == N_BSS
  175.     && symbolP->sy_value == local_bss_counter)
  176.     || ((symbolP->sy_type & N_TYPE) == N_UNDF
  177.     && symbolP->sy_value == 0))) {
  178.         symbolP->sy_value = local_bss_counter;
  179.         symbolP->sy_type  = N_BSS;
  180.         symbolP->sy_frag  = & bss_address_frag;
  181.         local_bss_counter += temp;
  182.     } else {
  183.     as_warn( "Ignoring attempt to re-define symbol from %d. to %d.",
  184.         symbolP->sy_value, local_bss_counter );
  185.     }
  186.     demand_empty_rest_of_line();
  187.     return;
  188. }
  189.  
  190. static void
  191. s_common()
  192. {
  193.     register char *name;
  194.     register char c;
  195.     register char *p;
  196.     register int temp;
  197.     register symbolS *    symbolP;
  198.  
  199.     name = input_line_pointer;
  200.     c = get_symbol_end();
  201.     /* just after name is now '\0' */
  202.     p = input_line_pointer;
  203.     *p = c;
  204.     SKIP_WHITESPACE();
  205.     if ( * input_line_pointer != ',' ) {
  206.     as_warn("Expected comma after symbol-name");
  207.     ignore_rest_of_line();
  208.     return;
  209.     }
  210.     input_line_pointer ++; /* skip ',' */
  211.     if ( (temp = get_absolute_expression ()) < 0 ) {
  212.     as_warn(".COMMon length (%d.) <0! Ignored.", temp);
  213.     ignore_rest_of_line();
  214.     return;
  215.     }
  216.     *p = 0;
  217.     symbolP = symbol_find_or_make (name);
  218.     *p = c;
  219.     if (   (symbolP->sy_type & N_TYPE) != N_UNDF ||
  220.         symbolP->sy_other != 0 || symbolP->sy_desc != 0) {
  221.     as_warn( "Ignoring attempt to re-define symbol");
  222.     ignore_rest_of_line();
  223.     return;
  224.     }
  225.     if (symbolP->sy_value) {
  226.     if (symbolP->sy_value != temp) {
  227.         as_warn( "Length of .comm \"%s\" is already %d. Not changed to %d.",
  228.         symbolP->sy_name, symbolP->sy_value, temp);
  229.     }
  230.     } else {
  231.     symbolP->sy_value = temp;
  232.     symbolP->sy_type |= N_EXT;
  233.     }
  234.     know(symbolP->sy_frag == &zero_address_frag);
  235.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  236.     p=input_line_pointer;
  237.     while(*p && *p!='\n')
  238.         p++;
  239.     c= *p;
  240.     *p='\0';
  241.     as_warn("bad .common segment: `%s'", input_line_pointer);
  242.     *p=c;
  243.     return;
  244.     }
  245.     input_line_pointer += 6;
  246.     demand_empty_rest_of_line();
  247.     return;
  248. }
  249.  
  250. static void
  251. s_seg()
  252. {
  253.  
  254.     if (strncmp(input_line_pointer, "\"text\"", 6) == 0) {
  255.     input_line_pointer += 6;
  256.     s_text();
  257.     return;
  258.     }
  259.     if (strncmp(input_line_pointer, "\"data\"", 6) == 0) {
  260.     input_line_pointer += 6;
  261.     s_data();
  262.     return;
  263.     }
  264.     if (strncmp(input_line_pointer, "\"data1\"", 7) == 0) {
  265.     input_line_pointer += 7;
  266.     s_data1();
  267.     return;
  268.     }
  269.     as_warn("Unknown segment type");
  270.     demand_empty_rest_of_line();
  271.     return;
  272. }
  273.  
  274. static void
  275. s_data1()
  276. {
  277.     subseg_new(SEG_DATA, 1);
  278.     demand_empty_rest_of_line();
  279.     return;
  280. }
  281.  
  282. static void
  283. s_proc()
  284. {
  285.     extern char is_end_of_line[];
  286.  
  287.     while (!is_end_of_line[*input_line_pointer]) {
  288.     ++input_line_pointer;
  289.     }
  290.     ++input_line_pointer;
  291.     return;
  292. }
  293.  
  294. static void
  295. s_sparc_align()
  296. {
  297.     register unsigned int temp;
  298.     register long int temp_fill;
  299.     unsigned int i;
  300.  
  301.     temp = get_absolute_expression ();
  302. #define MAX_ALIGNMENT (1 << 15)
  303.     if ( temp > MAX_ALIGNMENT ) {
  304.     as_warn("Alignment too large: %d. assumed.", temp = MAX_ALIGNMENT);
  305.     }
  306.  
  307.     /*
  308.      * For the sparc, `.align (1<<n)' actually means `.align n'
  309.      * so we have to convert it.
  310.      */
  311.     if (temp != 0) {
  312.     for (i = 0; (temp & 1) == 0; temp >>= 1, ++i)
  313.         ;
  314.     }
  315.     if (temp != 1) {
  316.     as_warn("Alignment not a power of 2");
  317.     }
  318.     temp = i;
  319.     if (*input_line_pointer == ',') {
  320.     input_line_pointer ++;
  321.     temp_fill = get_absolute_expression ();
  322.     } else {
  323.     temp_fill = 0;
  324.     }
  325.     /* Only make a frag if we HAVE to. . . */
  326.     if (temp && ! need_pass_2) {
  327.     frag_align (temp, (int)temp_fill);
  328.     }
  329.     demand_empty_rest_of_line();
  330.     return;
  331. }
  332.  
  333. /* This function is called once, at assembler startup time.  It should
  334.    set up all the tables, etc. that the MD part of the assembler will need.  */
  335. void
  336. md_begin()
  337. {
  338.   register char *retval = NULL;
  339.   int lose = 0;
  340.   register unsigned int i = 0;
  341.  
  342.   op_hash = hash_new();
  343.   if (op_hash == NULL)
  344.     as_fatal("Virtual memory exhausted");
  345.  
  346.   while (i < NUMOPCODES)
  347.     {
  348.       const char *name = sparc_opcodes[i].name;
  349.       retval = hash_insert(op_hash, name, &sparc_opcodes[i]);
  350.       if(retval != NULL && *retval != '\0')
  351.     {
  352.       fprintf (stderr, "internal error: can't hash `%s': %s\n",
  353.            sparc_opcodes[i].name, retval);
  354.       lose = 1;
  355.     }
  356.       do
  357.     {
  358.       if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
  359.         {
  360.           fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
  361.                sparc_opcodes[i].name, sparc_opcodes[i].args);
  362.           lose = 1;
  363.         }
  364.       ++i;
  365.     } while (i < NUMOPCODES
  366.          && !strcmp(sparc_opcodes[i].name, name));
  367.     }
  368.  
  369.   if (lose)
  370.     as_fatal ("Broken assembler.  No assembly attempted.");
  371.  
  372.   for (i = '0'; i < '8'; ++i)
  373.     octal[i] = 1;
  374.   for (i = '0'; i <= '9'; ++i)
  375.     toHex[i] = i - '0';
  376.   for (i = 'a'; i <= 'f'; ++i)
  377.     toHex[i] = i + 10 - 'a';
  378.   for (i = 'A'; i <= 'F'; ++i)
  379.     toHex[i] = i + 10 - 'A';
  380. }
  381.  
  382. void
  383. md_end()
  384. {
  385.     return;
  386. }
  387.  
  388. void
  389. md_assemble(str)
  390.     char *str;
  391. {
  392.     char *toP;
  393.     int rsd;
  394.  
  395.     assert(str);
  396.     sparc_ip(str);
  397.     toP = frag_more(4);
  398.     /* put out the opcode */
  399.     if (special_case != 0) {
  400.     assert(special_case == SPECIAL_CASE_SET):
  401.     special_case = 0;
  402.     assert(the_insn.reloc == RELOC_HI22);
  403.     /*
  404.      * We can have a number of cases here.
  405.      * 1.  Only high bits are set.  Use RELOC_HI22
  406.      * 2.  Only low bits are set.  Use RELOC_LO10
  407.      * 3.  Both high and low bits are set.  Generate 2 instructions.
  408.      * 4.  We might not be able to tell.  Generate 2 insns just to be safe.
  409.      */
  410.     if (the_insn.X_add_symbol == NULL
  411.         && the_insn.X_subtract_symbol == NULL) {
  412.         if ((the_insn.X_add_number & 0x3ff) == 0) {
  413.         /* only high bits are set */
  414.         goto foobar;
  415.         } else if ((the_insn.X_add_number & 0xfffffc00) == 0) {
  416.         /* only low bits are set */
  417.         the_insn.opcode = 0x80102000 | (rsd << 25) | (rsd << 14);
  418.         the_insn.reloc = RELOC_LO10;
  419.         goto foobar;
  420.         }
  421.     }
  422.     /*
  423.      * Either both high and low bits are set, or there are unresolved
  424.      * symbols so that it cannot be determined in one pass.
  425.      */
  426.     toP = frag_more(4);
  427.     rsd = (the_insn.opcode >> 25) & 0x1f;
  428.     the_insn.opcode = 0x80102000 | (rsd << 25) | (rsd << 14);
  429.     md_number_to_chars(toP, the_insn.opcode, 4);
  430.     fix_new(
  431.         frag_now,                           /* which frag */
  432.         (toP - frag_now->fr_literal),       /* where */
  433.         4,                                  /* size */
  434.         the_insn.exp.X_add_symbol,
  435.         the_insn.exp.X_subtract_symbol,
  436.         the_insn.exp.X_add_number,
  437.         the_insn.pcrel,
  438.         RELOC_LO10
  439.     );
  440.     return;
  441.     }
  442.     md_number_to_chars(toP, the_insn.opcode, 4);
  443.     /* put out the symbol-dependent stuff */
  444.     if (the_insn.reloc != NO_RELOC) {
  445.         fix_new(
  446.             frag_now,                           /* which frag */
  447.         (toP - frag_now->fr_literal),       /* where */
  448.         4,                                  /* size */
  449.         the_insn.exp.X_add_symbol,
  450.         the_insn.exp.X_subtract_symbol,
  451.         the_insn.exp.X_add_number,
  452.         the_insn.pcrel,
  453.         the_insn.reloc
  454.         );
  455.     }
  456. }
  457.  
  458. static void
  459. sparc_ip(str)
  460.     char *str;
  461. {
  462.     char *s;
  463.     const char *args;
  464.     char c;
  465.     unsigned long i;
  466.     struct sparc_opcode *insn;
  467.     char *argsStart;
  468.     unsigned long   opcode;
  469.     unsigned int mask;
  470.     int match = FALSE;
  471.     int comma = 0;
  472.  
  473.     for (s = str; islower(*s) || (*s >= '0' && *s <= '3'); ++s)
  474.     ;
  475.     switch (*s) {
  476.  
  477.     case '\0':
  478.     break;
  479.  
  480.     case ',':
  481.     comma = 1;
  482.  
  483.     /*FALLTHROUGH*/
  484.  
  485.     case ' ':
  486.     *s++ = '\0';
  487.     break;
  488.  
  489.     default:
  490.         as_warn("Unknown opcode: `%s'", str);
  491.         exit(1);
  492.     }
  493.     if ((insn = (struct sparc_opcode *) hash_find(op_hash, str)) == NULL) {
  494.     as_warn("`%s' not in hash table.", str);
  495.     return;
  496.     }
  497.     if (comma) {
  498.     *--s = ',';
  499.     }
  500.     argsStart = s;
  501.     for (;;) {
  502.     opcode = insn->match;
  503.     bzero(&the_insn, sizeof(the_insn));
  504.     the_insn.reloc = NO_RELOC;
  505.  
  506.     /*
  507.      * Build the opcode, checking as we go to make
  508.      * sure that the operands match
  509.      */
  510.     for (args = insn->args; ; ++args) {
  511.         switch (*args) {
  512.  
  513.         case '\0':  /* end of args */
  514.         if (*s == '\0') {
  515.             match = TRUE;
  516.         }
  517.         break;
  518.  
  519.         case '+':
  520.         if (*s == '+') {
  521.             ++s;
  522.             continue;
  523.         }
  524.         if (*s == '-') {
  525.             continue;
  526.         }
  527.         break;
  528.  
  529.         case '[':   /* these must match exactly */
  530.         case ']':
  531.         case ',':
  532.         case ' ':
  533.         if (*s++ == *args)
  534.             continue;
  535.         break;
  536.  
  537.         case '#':   /* must be at least one digit */
  538.         if (isdigit(*s++)) {
  539.             while (isdigit(*s)) {
  540.             ++s;
  541.             }
  542.             continue;
  543.         }
  544.         break;
  545.  
  546.         case 'C':   /* coprocessor state register */
  547.         if (strncmp(s, "%csr", 4) == 0) {
  548.             s += 4;
  549.             continue;
  550.         }
  551.         break;
  552.  
  553.         case 'b':    /* next operand is a coprocessor register */
  554.         case 'c':
  555.         case 'D':
  556.             if (*s++ == '%' && *s++ == 'c' && isdigit(*s)) {
  557.             mask = *s++;
  558.             if (isdigit(*s)) {
  559.             mask = 10 * (mask - '0') + (*s++ - '0');
  560.             if (mask >= 32) {
  561.                 break;
  562.             }
  563.             } else {
  564.             mask -= '0';
  565.             }
  566.             switch (*args) {
  567.  
  568.             case 'b':
  569.             opcode |= mask << 14;
  570.             continue;
  571.  
  572.             case 'c':
  573.             opcode |= mask;
  574.             continue;
  575.  
  576.             case 'D':
  577.             opcode |= mask << 25;
  578.             continue;
  579.             }
  580.         }
  581.         break;
  582.  
  583.         case 'r':   /* next operand must be a register */
  584.         case '1':
  585.         case '2':
  586.         case 'd':
  587.         if (*s++ == '%') {
  588.             switch (c = *s++) {
  589.  
  590.             case 'f':   /* frame pointer */
  591.                 if (*s++ == 'p') {
  592.                 mask = 0x1e;
  593.                 break;
  594.             }
  595.             goto error;
  596.  
  597.             case 'g':   /* global register */
  598.             if (isoctal(c = *s++)) {
  599.                 mask = c - '0';
  600.                 break;
  601.             }
  602.             goto error;
  603.  
  604.             case 'i':   /* in register */
  605.             if (isoctal(c = *s++)) {
  606.                 mask = c - '0' + 24;
  607.                 break;
  608.             }
  609.             goto error;
  610.  
  611.             case 'l':   /* local register */
  612.             if (isoctal(c = *s++)) {
  613.                 mask= (c - '0' + 16) ;
  614.                 break;
  615.             }
  616.             goto error;
  617.  
  618.             case 'o':   /* out register */
  619.             if (isoctal(c = *s++)) {
  620.                 mask= (c - '0' + 8) ;
  621.                 break;
  622.             }
  623.             goto error;
  624.  
  625.             case 's':   /* stack pointer */
  626.                 if (*s++ == 'p') {
  627.                 mask= 0xe;
  628.                 break;
  629.             }
  630.             goto error;
  631.  
  632.             case 'r': /* any register */
  633.                 if (!isdigit(c = *s++)) {
  634.                 goto error;
  635.             }
  636.             /* FALLTHROUGH */
  637.             case '0': case '1': case '2': case '3': case '4':
  638.             case '5': case '6': case '7': case '8': case '9':
  639.             if (isdigit(*s)) {
  640.                 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32) {
  641.                 goto error;
  642.                 }
  643.             } else {
  644.                 c -= '0';
  645.             }
  646.             mask= c;
  647.             break;
  648.  
  649.             default:
  650.             goto error;
  651.             }
  652.             /*
  653.              * Got the register, now figure out where
  654.              * it goes in the opcode.
  655.              */
  656.             switch (*args) {
  657.  
  658.             case '1':
  659.             opcode |= mask << 14;
  660.             continue;
  661.  
  662.             case '2':
  663.             opcode |= mask;
  664.             continue;
  665.  
  666.             case 'd':
  667.             opcode |= mask << 25;
  668.             continue;
  669.  
  670.             case 'r':
  671.             opcode |= (mask << 25) | (mask << 14);
  672.             continue;
  673.             }
  674.         }
  675.         break;
  676.  
  677.         case 'e':    /* next operand is a floating point register */
  678.         case 'f':
  679.         case 'g':
  680.             if (*s++ == '%' && *s++ == 'f' && isdigit(*s)) {
  681.             mask = *s++;
  682.             if (isdigit(*s)) {
  683.             mask = 10 * (mask - '0') + (*s++ - '0');
  684.             if (mask >= 32) {
  685.                 break;
  686.             }
  687.             } else {
  688.             mask -= '0';
  689.             }
  690.             switch (*args) {
  691.  
  692.             case 'e':
  693.             opcode |= mask << 14;
  694.             continue;
  695.  
  696.             case 'f':
  697.             opcode |= mask;
  698.             continue;
  699.  
  700.             case 'g':
  701.             opcode |= mask << 25;
  702.             continue;
  703.             }
  704.         }
  705.         break;
  706.  
  707.         case 'F':
  708.         if (strncmp(s, "%fsr", 4) == 0) {
  709.             s += 4;
  710.             continue;
  711.         }
  712.         break;
  713.  
  714.         case 'h':       /* high 22 bits */
  715.             the_insn.reloc = RELOC_HI22;
  716.         goto immediate;
  717.  
  718.         case 'l':   /* 22 bit PC relative immediate */
  719.             the_insn.reloc = RELOC_WDISP22;
  720.         the_insn.pcrel = 1;
  721.         goto immediate;
  722.  
  723.         case 'L':   /* 30 bit immediate */
  724.             the_insn.reloc = RELOC_WDISP30;
  725.         the_insn.pcrel = 1;
  726.         goto immediate;
  727.  
  728.         case 'i':   /* 13 bit immediate */
  729.             the_insn.reloc = RELOC_BASE13;
  730.  
  731.         /*FALLTHROUGH*/
  732.  
  733.         immediate:
  734.         if(*s==' ')
  735.           s++;
  736.         if (*s == '%') {
  737.             if ((c = s[1]) == 'h' && s[2] == 'i') {
  738.             the_insn.reloc = RELOC_HI22;
  739.             s+=3;
  740.             } else if (c == 'l' && s[2] == 'o') {
  741.             the_insn.reloc = RELOC_LO10;
  742.             s+=3;
  743.             } else
  744.                 break;
  745.         }
  746.         /* Note that if the getExpression() fails, we will still have
  747.            created U entries in the symbol table for the 'symbols'
  748.            in the input string.  Try not to create U symbols for
  749.            registers, etc. */
  750.         {
  751.             /* This stuff checks to see if the expression ends
  752.                in +%reg If it does, it removes the register from
  753.                the expression, and re-sets 's' to point to the
  754.                right place */
  755.  
  756.             char *s1;
  757.  
  758.             for(s1=s;*s1 && *s1!=','&& *s1!=']';s1++)
  759.                 ;
  760.  
  761.             if(s1!=s && isdigit(s1[-1])) {
  762.                 if(s1[-2]=='%' && s1[-3]=='+') {
  763.                     s1-=3;
  764.                     *s1='\0';
  765.                     (void)getExpression(s);
  766.                     *s1='+';
  767.                     s=s1;
  768.                     continue;
  769.                 } else if(index("goli0123456789",s1[-2]) && s1[-3]=='%' && s1[-4]=='+') {
  770.                     s1-=4;
  771.                     *s1='\0';
  772.                     (void)getExpression(s);
  773.                     *s1='+';
  774.                     s=s1;
  775.                     continue;
  776.                 }
  777.             }
  778.         }
  779.         (void)getExpression(s);
  780.         s = expr_end;
  781.         continue;
  782.  
  783.         case 'a':
  784.         if (*s++ == 'a') {
  785.             opcode |= ANNUL;
  786.             continue;
  787.         }
  788.         break;
  789.  
  790.         case 'A':       /* alternate space */
  791.         if (isdigit(*s)) {
  792.             long num = 0;
  793.  
  794.             if (s[0] == '0' && s[1] == 'x') {
  795.             s += 2;
  796.             while (isxdigit(*s)) {
  797.                 if (isdigit(*s)) {
  798.                 num = num*16 + *s - '0';
  799.                 } else if (isupper(*s)) {
  800.                 num = num*16 + *s - 'A' + 10;
  801.                 } else {
  802.                 num = num*16 + *s - 'a' + 10;
  803.                 }
  804.                 ++s;
  805.             }
  806.             } else {
  807.             while (isdigit(*s)) {
  808.                 num= num*10 + *s-'0';
  809.                 ++s;
  810.             }
  811.             }
  812.             opcode |= num<<5;
  813.             continue;
  814.         }
  815.         break;
  816.         /* abort(); */
  817.  
  818.         case 'p':
  819.         if (strncmp(s, "%psr", 4) == 0) {
  820.             s += 4;
  821.             continue;
  822.         }
  823.         break;
  824.  
  825.         case 'q':   /* floating point queue */
  826.         if (strncmp(s, "%fq", 3) == 0) {
  827.             s += 3;
  828.             continue;
  829.         }
  830.         break;
  831.  
  832.         case 'Q':   /* coprocessor queue */
  833.         if (strncmp(s, "%cq", 3) == 0) {
  834.             s += 3;
  835.             continue;
  836.         }
  837.         break;
  838.  
  839.         case 'S':
  840.         if (strcmp(str, "set") == 0) {
  841.             special_case = SPECIAL_CASE_SET;
  842.             continue;
  843.         }
  844.         break;
  845.  
  846.         case 't':
  847.         if (strncmp(s, "%tbr", 4) != 0)
  848.             break;
  849.         s += 4;
  850.         continue;
  851.  
  852.         case 'w':
  853.         if (strncmp(s, "%wim", 4) != 0)
  854.             break;
  855.         s += 4;
  856.         continue;
  857.  
  858.         case 'y':
  859.         if (strncmp(s, "%y", 2) != 0)
  860.             break;
  861.         s += 2;
  862.         continue;
  863.  
  864.         default:
  865.         abort();
  866.         }
  867.         break;
  868.     }
  869.     error:
  870.     if (match == FALSE)
  871.       {
  872.         /* Args don't match.  */
  873.         if (&insn[1] - sparc_opcodes < NUMOPCODES
  874.         && !strcmp(insn->name, insn[1].name))
  875.           {
  876.         ++insn;
  877.         s = argsStart;
  878.         continue;
  879.           }
  880.         else
  881.           {
  882.         as_warn("Illegal operands");
  883.         return;
  884.           }
  885.       }
  886.     break;
  887.     }
  888.  
  889.     the_insn.opcode = opcode;
  890.     return;
  891. }
  892.  
  893. static int
  894. getExpression(str)
  895.     char *str;
  896. {
  897.     char *save_in;
  898.     segT seg;
  899.  
  900.     save_in = input_line_pointer;
  901.     input_line_pointer = str;
  902.     switch (seg = expression(&the_insn.exp)) {
  903.  
  904.     case SEG_ABSOLUTE:
  905.     case SEG_TEXT:
  906.     case SEG_DATA:
  907.     case SEG_BSS:
  908.     case SEG_UNKNOWN:
  909.     case SEG_DIFFERENCE:
  910.     case SEG_BIG:
  911.     case SEG_NONE:
  912.     break;
  913.  
  914.     default:
  915.     the_insn.error = "bad segment";
  916.     expr_end = input_line_pointer;
  917.     input_line_pointer=save_in;
  918.     return 1;
  919.     }
  920.     expr_end = input_line_pointer;
  921.     input_line_pointer = save_in;
  922.     return 0;
  923. }
  924.  
  925.  
  926. #define MAX_LITTLENUMS 6
  927.  
  928. /*
  929.     This is identical to the md_atof in m68k.c.  I think this is right,
  930.     but I'm not sure.
  931.  
  932.    Turn a string in input_line_pointer into a floating point constant of type
  933.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  934.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  935.  */
  936. char *
  937. md_atof(type,litP,sizeP)
  938.     char type;
  939.     char *litP;
  940.     int *sizeP;
  941. {
  942.     int    prec;
  943.     LITTLENUM_TYPE words[MAX_LITTLENUMS];
  944.     LITTLENUM_TYPE *wordP;
  945.     char    *t;
  946.     char    *atof_m68k();
  947.  
  948.     switch(type) {
  949.  
  950.     case 'f':
  951.     case 'F':
  952.     case 's':
  953.     case 'S':
  954.     prec = 2;
  955.     break;
  956.  
  957.     case 'd':
  958.     case 'D':
  959.     case 'r':
  960.     case 'R':
  961.     prec = 4;
  962.     break;
  963.  
  964.     case 'x':
  965.     case 'X':
  966.     prec = 6;
  967.     break;
  968.  
  969.     case 'p':
  970.     case 'P':
  971.     prec = 6;
  972.     break;
  973.  
  974.     default:
  975.     *sizeP=0;
  976.     return "Bad call to MD_ATOF()";
  977.     }
  978.     t=atof_m68k(input_line_pointer,type,words);
  979.     if(t)
  980.     input_line_pointer=t;
  981.     *sizeP=prec * sizeof(LITTLENUM_TYPE);
  982.     for(wordP=words;prec--;) {
  983.     md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
  984.     litP+=sizeof(LITTLENUM_TYPE);
  985.     }
  986.     return "";    /* Someone should teach Dean about null pointers */
  987. }
  988.  
  989. /*
  990.  * Write out big-endian.
  991.  */
  992. void
  993. md_number_to_chars(buf,val,n)
  994.     char *buf;
  995.     long val;
  996.     int n;
  997. {
  998.  
  999.     switch(n) {
  1000.  
  1001.     case 4:
  1002.     *buf++ = val >> 24;
  1003.     *buf++ = val >> 16;
  1004.     case 2:
  1005.     *buf++ = val >> 8;
  1006.     case 1:
  1007.     *buf = val;
  1008.     break;
  1009.  
  1010.     default:
  1011.     abort();
  1012.     }
  1013.     return;
  1014. }
  1015.  
  1016. void
  1017. md_number_to_imm(buf,val,n, fixP, seg_type)
  1018.     char *buf;
  1019.     long val;
  1020.     int n;
  1021.     fixS *fixP;
  1022.     int seg_type;
  1023. {
  1024.     if (seg_type != N_TEXT || fixP->fx_r_type == (int) NO_RELOC) {
  1025.     switch (n) {
  1026.     case 1:
  1027.         *buf = val;
  1028.         break;
  1029.     case 2:
  1030.         *buf++ = (val>>8);
  1031.         *buf = val;
  1032.         break;
  1033.     case 4:
  1034.         *buf++ = (val>>24);
  1035.         *buf++ = (val>>16);
  1036.         *buf++ = (val>>8);
  1037.         *buf = val;
  1038.         break;
  1039.     default:
  1040.         abort();
  1041.     }
  1042.     return;
  1043.     }
  1044.  
  1045.     assert(n == 4);
  1046.     assert(fixP->fx_r_type < NO_RELOC);
  1047.  
  1048.     /*
  1049.      * This is a hack.  There should be a better way to
  1050.      * handle this.
  1051.      */
  1052.     if (fixP->fx_r_type == (int) RELOC_WDISP30 && fixP->fx_addsy) {
  1053.         val += fixP->fx_where + fixP->fx_frag->fr_address;
  1054.     }
  1055.  
  1056.     switch (fixP->fx_r_type) {
  1057.  
  1058.     case RELOC_32:
  1059.     buf[0] = val >> 24;
  1060.     buf[1] = val >> 16;
  1061.     buf[2] = val >> 8;
  1062.     buf[3] = val;
  1063.     break;
  1064.  
  1065. #if 0
  1066.     case RELOC_8:         /* These don't seem to ever be needed. */
  1067.     case RELOC_16:
  1068.     case RELOC_DISP8:
  1069.     case RELOC_DISP16:
  1070.     case RELOC_DISP32:
  1071. #endif
  1072.     case RELOC_WDISP30:
  1073.     val = (val >>= 2) + 1;
  1074.     buf[0] |= (val >> 24) & 0x3f;
  1075.     buf[1]= (val >> 16);
  1076.     buf[2] = val >> 8;
  1077.     buf[3] = val;
  1078.     break;
  1079.  
  1080.     case RELOC_HI22:
  1081.     if(!fixP->fx_addsy) {
  1082.       buf[1] |= (val >> 26) & 0x3f;
  1083.       buf[2] = val >> 18;
  1084.       buf[3] = val >> 10;
  1085.     } else {
  1086.       buf[2]=0;
  1087.       buf[3]=0;
  1088.     }
  1089.     break;
  1090. #if 0
  1091.     case RELOC_22:
  1092.     case RELOC_13:
  1093. #endif
  1094.     case RELOC_LO10:
  1095.     if(!fixP->fx_addsy) {
  1096.       buf[2] |= (val >> 8) & 0x03;
  1097.       buf[3] = val;
  1098.     } else
  1099.       buf[3]=0;
  1100.     break;
  1101. #if 0
  1102.     case RELOC_SFA_BASE:
  1103.     case RELOC_SFA_OFF13:
  1104.     case RELOC_BASE10:
  1105. #endif
  1106.     case RELOC_BASE13:
  1107.     buf[2] |= (val >> 8) & 0x1f;
  1108.     buf[3] = val;
  1109.     break;
  1110.  
  1111.     case RELOC_WDISP22:
  1112.     val = (val >>= 2) + 1;
  1113.     /* FALLTHROUGH */
  1114.     case RELOC_BASE22:
  1115.     buf[1] |= (val >> 16) & 0x3f;
  1116.     buf[2] = val >> 8;
  1117.     buf[3] = val;
  1118.     break;
  1119.  
  1120. #if 0
  1121.     case RELOC_PC10: 
  1122.     case RELOC_PC22: 
  1123.     case RELOC_JMP_TBL:
  1124.     case RELOC_SEGOFF16:
  1125.     case RELOC_GLOB_DAT:
  1126.     case RELOC_JMP_SLOT: 
  1127.     case RELOC_RELATIVE:
  1128. #endif
  1129.  
  1130.     case NO_RELOC:
  1131.     default:
  1132.     as_warn("bad relocation type: 0x%02x", fixP->fx_r_type);
  1133.     break;
  1134.     }
  1135.     return;
  1136. }
  1137.  
  1138. /* should never be called for sparc */
  1139. void
  1140. md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1141.     char *ptr;
  1142.     long from_addr, to_addr;
  1143. {
  1144.     fprintf(stderr, "sparc_create_short_jmp\n");
  1145.     abort();
  1146. }
  1147.  
  1148. /* should never be called for sparc */
  1149. void
  1150. md_number_to_disp(buf,val,n)
  1151.     char    *buf;
  1152.     long    val;
  1153. {
  1154.     fprintf(stderr, "md_number_to_disp\n");
  1155.     abort();
  1156. }
  1157.  
  1158. /* should never be called for sparc */
  1159. void
  1160. md_number_to_field(buf,val,fix)
  1161.     char *buf;
  1162.     long val;
  1163. #ifdef __STDC__
  1164.     void *fix;
  1165. #else
  1166.     char *fix;
  1167. #endif
  1168. {
  1169.     fprintf(stderr, "sparc_number_to_field\n");
  1170.     abort();
  1171. }
  1172.  
  1173. void
  1174. md_ri_to_chars(ri_p, ri)
  1175.     struct reloc_info_sparc *ri_p, ri;
  1176. {
  1177.     unsigned long n;
  1178.     char *p;
  1179.  
  1180.     p = (char *) ri_p;
  1181.     n = ri_p->r_address;
  1182.     p[3] = n;
  1183.     p[2] = n >> 8;
  1184.     p[1] = n >> 16;
  1185.     p[0] = n >> 24;
  1186.     n = (ri_p->r_index << 8) + (ri_p->r_extern << 7) + ((int) ri_p->r_type);
  1187.     p[7] = n;
  1188.     p[6] = n >> 8;
  1189.     p[5] = n >> 16;
  1190.     p[4] = n >> 24;
  1191.     n = ri_p->r_addend;
  1192.     p[11] = n;
  1193.     p[10] = n >> 8;
  1194.     p[9] = n >> 16;
  1195.     p[8] = n >> 24;
  1196.     return;
  1197. }
  1198.  
  1199. /* should never be called for sparc */
  1200. void
  1201. md_convert_frag(fragP)
  1202.     register fragS *fragP;
  1203. {
  1204.     fprintf(stderr, "sparc_convert_frag\n");
  1205.     abort();
  1206. }
  1207.  
  1208. /* should never be called for sparc */
  1209. void
  1210. md_create_long_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1211.     char    *ptr;
  1212.     long    from_addr,
  1213.             to_addr;
  1214.     fragS    *frag;
  1215.     symbolS    *to_symbol;
  1216. {
  1217.     fprintf(stderr, "sparc_create_long_jump\n");
  1218.     abort();
  1219. }
  1220.  
  1221. /* should never be called for sparc */
  1222. int
  1223. md_estimate_size_before_relax(fragP, segtype)
  1224.     register fragS *fragP;
  1225. {
  1226.     fprintf(stderr, "sparc_estimate_size_before_relax\n");
  1227.     abort();
  1228.     return 0;
  1229. }
  1230.  
  1231. #if 0
  1232. /* for debugging only */
  1233. static void
  1234. print_insn(insn)
  1235.     struct sparc_it *insn;
  1236. {
  1237.     char *Reloc[] = {
  1238.     "RELOC_8",
  1239.     "RELOC_16",
  1240.     "RELOC_32",
  1241.     "RELOC_DISP8",
  1242.     "RELOC_DISP16",
  1243.     "RELOC_DISP32",
  1244.     "RELOC_WDISP30",
  1245.     "RELOC_WDISP22",
  1246.     "RELOC_HI22",
  1247.     "RELOC_22",
  1248.     "RELOC_13",
  1249.     "RELOC_LO10",
  1250.     "RELOC_SFA_BASE",
  1251.     "RELOC_SFA_OFF13",
  1252.     "RELOC_BASE10",
  1253.     "RELOC_BASE13",
  1254.     "RELOC_BASE22",
  1255.     "RELOC_PC10",
  1256.     "RELOC_PC22",
  1257.     "RELOC_JMP_TBL",
  1258.     "RELOC_SEGOFF16",
  1259.     "RELOC_GLOB_DAT",
  1260.     "RELOC_JMP_SLOT",
  1261.     "RELOC_RELATIVE",
  1262.     "NO_RELOC"
  1263.     };
  1264.  
  1265.     if (insn->error) {
  1266.     fprintf(stderr, "ERROR: %s\n");
  1267.     }
  1268.     fprintf(stderr, "opcode=0x%08x\n", insn->opcode);
  1269.     fprintf(stderr, "reloc = %s\n", Reloc[insn->reloc]);
  1270.     fprintf(stderr, "exp =  {\n");
  1271.     fprintf(stderr, "\t\tX_add_symbol = %s\n",
  1272.     insn->exp.X_add_symbol ?
  1273.     (insn->exp.X_add_symbol->sy_name ? 
  1274.     insn->exp.X_add_symbol->sy_name : "???") : "0");
  1275.     fprintf(stderr, "\t\tX_sub_symbol = %s\n",
  1276.     insn->exp.X_subtract_symbol ?
  1277.         (insn->exp.X_subtract_symbol->sy_name ? 
  1278.             insn->exp.X_subtract_symbol->sy_name : "???") : "0");
  1279.     fprintf(stderr, "\t\tX_add_number = %d\n",
  1280.     insn->exp.X_add_number);
  1281.     fprintf(stderr, "}\n");
  1282.     return;
  1283. }
  1284. #endif
  1285.  
  1286. /*
  1287.  * Sparc relocations are completely different, so it needs
  1288.  * this machine dependent routine to emit them.
  1289.  */
  1290. void
  1291. emit_relocations(fixP, segment_address_in_file)
  1292.     register fixS *fixP;
  1293.     relax_addressT segment_address_in_file;
  1294. {
  1295.     struct reloc_info_sparc ri;
  1296.     register symbolS *symbolP;
  1297.     extern char *next_object_file_charP;
  1298.     long add_number;
  1299.  
  1300.     bzero((char *) &ri, sizeof(ri));
  1301.     for (; fixP; fixP = fixP->fx_next) {
  1302.  
  1303.     if (fixP->fx_r_type >= (int) NO_RELOC) {
  1304.         fprintf(stderr, "fixP->fx_r_type = %d\n", fixP->fx_r_type);
  1305.         abort();
  1306.     }
  1307.  
  1308.     if ((symbolP = fixP->fx_addsy) != NULL) {
  1309.         ri.r_address = fixP->fx_frag->fr_address +
  1310.             fixP->fx_where - segment_address_in_file;
  1311.         if ((symbolP->sy_type & N_TYPE) == N_UNDF) {
  1312.         ri.r_extern = 1;
  1313.         ri.r_index = symbolP->sy_number;
  1314.         } else {
  1315.         ri.r_extern = 0;
  1316.         ri.r_index = symbolP->sy_type & N_TYPE;
  1317.         }
  1318.         if (symbolP && symbolP->sy_frag) {
  1319.         ri.r_addend = symbolP->sy_frag->fr_address;
  1320.         }
  1321.         ri.r_type = (enum reloc_type) fixP->fx_r_type;
  1322.         if (fixP->fx_pcrel) {
  1323. /*        ri.r_addend -= fixP->fx_where;          */
  1324.         ri.r_addend -= ri.r_address;            
  1325.         } else {
  1326.         ri.r_addend = fixP->fx_addnumber;
  1327.         }
  1328.         md_ri_to_chars((char *) &ri, ri);
  1329.         append(&next_object_file_charP, (char *)& ri, sizeof(ri));
  1330.     }
  1331.     }
  1332.     return;
  1333. }
  1334.  
  1335. int
  1336. md_parse_option(argP,cntP,vecP)
  1337.     char **argP;
  1338.     int *cntP;
  1339.     char ***vecP;
  1340. {
  1341.     return 1;
  1342. }
  1343.  
  1344.